class Solution {//leetcode417——太平洋的水流问题
private:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    void dfs(vector<vector<int>>& heights,int i,int j,vector<vector<bool>>& ocean){
        ocean[i][j]=true;
        for(int k=0;k<4;k++){
            int y=i+dy[k];
            int x=j+dx[k];
            if(x>=0&&x<n&&y>=0&&y<m&&heights[i][j]<=heights[y][x]&&ocean[y][x]==false){
                dfs(heights,y,x,ocean);
            }
        }
    }
public:
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        m=heights.size(),n=heights[0].size();
        p_ocean.resize(m,vector<bool>(n,false));
        t_ocean.resize(m,vector<bool>(n,false));
        for(int i=0;i<m;i++){
            if(p_ocean[i][0]==false)
                dfs(heights,i,0,p_ocean);
            if(t_ocean[i][n-1]==false)
                dfs(heights,i,n-1,t_ocean);
        }

        for(int j=0;j<n;j++){
            if(p_ocean[0][j]==false)
                dfs(heights,0,j,p_ocean);
            if(t_ocean[m-1][j]==false){
                dfs(heights,m-1,j,t_ocean);
            }
        }

        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++){
                if(p_ocean[i][j]&&t_ocean[i][j]){
                    ret.push_back(vector<int>{i,j});
                }
            }
        return ret;
    }
private:
    vector<vector<bool>> p_ocean;//流向太平洋的水
    vector<vector<bool>> t_ocean;//流向大西洋的水
    vector<vector<int>> ret;
    int m;
    int n;
};